/*
    Copyright (C) 2011 Rodrigo Castaño - Hugo Arregui, FuDePAN

    This file is part of the Prot-Filer Library

    Prot-Filer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Prot-Filer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Prot-Filer.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef FRAGMENTS_H
#define FRAGMENTS_H
#include <fstream>
#include <string>
#include <mili/mili.h>
#include "prot-filer/base.h"
#include "prot-filer/backbones_utils.h"
#include "prot-filer/read_utils.h"

namespace prot_filer
{
class FragmentsAnglesReaderImple : public FragmentsAnglesReader, public FormatMixin, private mili::NonCopyable
{
public:
    FragmentsAnglesReaderImple() :
        angles_data(NULL),
        angles_mapping(NULL),
        fragments(NULL),
        _atoms_in_a_protein(0),
        eof(false)
    {}
    ~FragmentsAnglesReaderImple()
    {
        delete angles_data;
        delete angles_mapping;
        delete fragments;
    }
    bool open(Fragments* _fragments, const std::string& name)
    {
        fragments = _fragments;
        eof = false;
        return FormatMixin::open(name);
    }
    void close()
    {
        FormatMixin::close();
    }
    bool read(AnglesData& protein);
    AnglesMapping* get_angles_mapping() const
    {
        return angles_mapping;
    }
    size_t get_atom_number() const
    {
        return _atoms_in_a_protein;
    }
private:
    bool _open(const std::string& name);
    void _close()
    {
        file.close();
    }
private:
    AnglesData* angles_data;
    AnglesMapping* angles_mapping;
    Fragments* fragments;
    int _atoms_in_a_protein;
    std::fstream file;
    CompressedHelper h;
    bool eof;
};

class FragmentsWriter : public FormatWriter
{
public:
    void write(const unsigned int fragment_size, const FragmentIds& ids, const AnglesData& angles_data);
private:
    std::fstream file;
    mili::FirstTimeFlag first_time;
    bool _open(const std::string& name);
    void _close();
    CompressedHelper h;
};
}
#endif
